package com.peng.leetcode.tree;

/**
 * Rob
 * <p>
 * 198. 打家劫舍
 *
 * @author: lupeng6
 * @create: 2021/2/18 17:11
 */
public class Rob {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 1};
        int rob = new Rob().rob(nums);
        System.out.println(rob);
    }

    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        dfs(nums, 0, 0, true);
        dfs(nums, 0, 0, false);
        return max;
    }

    private int max = Integer.MIN_VALUE;

    private void dfs(int[] nums, int sum, int index, boolean flag) {
        if (index == nums.length) {
            max = Math.max(max, sum);
            return;
        }
        if (flag) {
            sum += nums[index];
        }
        if (!flag) {
            dfs(nums, sum, index + 1, true);
        }
        dfs(nums, sum, index + 1, false);
    }
}
